// import()介绍：es2020提案，
/*
import()可运行时加载，异步加载，按需加载
import()返回一个promise对象
// eg:
import('./myModule.js')
.then(({export1, export2}) => {
  // ...·
});

import命令能够接受什么参数，import()函数就能接受什么参数，两者区别主要是后者为动态加载。
*/

require.ensure = (chunkId) => {
  let promises = [];
  require.find.jsonp(chunkId, promises);//在jsonp里会创建promise,并且添加到promises数组里
  return Promise.all(promises);
}
require.publicPath = '';//资源文件的访问路径,默认是空串
require.unionFileName = (chunkId) => {
  return "" + chunkId + ".js";//hello.js
};

//已经安装或者说加载好的chunk,也可能是加载中的
//0表示已经加载就绪
var installedChunks = {
  "main": 0,
  //"title":[resolve, reject,promise]
};


require.find.jsonp = (chunkId, promiees) => {
  var installedChunkData;
  let promise = new Promise((resolve, reject) => {
    // 将promise的resolve和reject存起来，需要用的时候在调用，这种思想很厉害，以前都是直接在 new Promise里执行了
    installedChunkData = installedChunks[chunkId] = [resolve, reject];
  });
  installedChunkData[2] = promise;
  promises.push(promise);
  var url = require.publicPath + require.unionFileName(chunkId);// hello.js
  require.load(url)
}
require.load = (url) => {
  let script = document.createElement('script');
  script.src = url;
  document.head.appendChild(script);//一旦append之后,浏览器会立刻请求对应脚本 引入的是打包后要异步加载的脚本( hello.main.js)
  // 要注意的是下面定义在self上的push方法，异步脚本中会使用这个方法
}

//chunkIds已经取回来的代码块 
var webpackJsonpCallback = ([chunkIds, moreModules]) => {
  var resolves = [];
  for (let i = 0; i < chunkIds.length; i++) {
    let chunkId = chunkIds[i];//title
    //找到这个代码块对应的installedChunkData,添加这个里面resolve.
    resolves.push(installedChunks[chunkId][0]);//把chunk对应的promise的resolve方法添加到resolves数组里
    installedChunks[chunkId] = 0;//已经 加载完成了
  }
  for (let moduleId in moreModules) {
    modules[moduleId] = moreModules[moduleId];
  }
  while (resolves.length) {
    resolves.shift()();//让promise的resolve执行,让promise成功
  }
}

var chunkLoadingGlobal = self["webpackChunk_2_bundle"] = self["webpackChunk_2_bundle"] || [];
chunkLoadingGlobal.push = webpackJsonpCallback; //给self[xxx]添加了push方法

// hello.main.js中用到push（webpackJsonpCallback）方法，将hello.js导出的变量添加到modules上，执行存起来的promise下的resolve
// 

require.ensure('hello')
  .then(require.bind(require, './src/hello.js'))
  .then(result => {
    console.log(result.default);
    console.log(result);
  })